<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>函数的练习(function practice)</title>
</head>
<body>

  <script>
    // 1. 计算三角形面积
    function getTriangleArea(a, b, c) {
      let p = (a + b + c) / 2;
      let area = Math.sqrt(p * (p - a) * (p - b) * (p - c));
      return area;
    }
    let area1 = getTriangleArea(3, 4, 5);
    console.log("1. 三角形面积：" + area1);

    // 2. 计算圆的面积
    function getCircleArea(r) {
      let area = Math.PI * r * r;
      return area;
    }
    let area2 = getCircleArea(2);
    console.log("2. 圆的面积：" + area2);

    // 3. 计算两个数的最大值
    function getMax(a, b) {
      return a > b ? a : b;
    }
    let max = getMax(10, 20);
    console.log("3. 最大值：" + max);

    // 4. 冒泡排序
    function bubbleSort(arr) {
      for (let i = 0; i < arr.length - 1; i++) {
        for (let j = 0; j < arr.length - 1 - i; j++) {
          if (arr[j] > arr[j + 1]) {
            let temp = arr[j];
            arr[j] = arr[j + 1]; 
            arr[j + 1] = temp;
          }
        }
      }
      return arr;
    }
    let arr = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
    let sortedArr = bubbleSort(arr);
    console.log("4. 冒泡排序：" + sortedArr);

    // 5. 计算1~n之和
    function getSum(n) {
      if (n <= 0) return false
      let sum = 0;
      for (let i = 1; i <= n; i++) {
        sum += i
      }
      return sum
    }

    let sum = getSum(100);
    console.log("5. 1~n之和：" + sum);

    // 6. 判断一个数是否是质数
    function isPrime(num) {
      if (num <= 1) return false;
      for (let i = 2; i < num; i++) {
        if (num % i === 0) {
          return false;
        }
      }
      return true;
    }
    let isPrimeNum = isPrime(19);
    console.log("6. 是否是质数：" + isPrimeNum);

    // 7. 数字格式化
    function formatNumber(num) {
      return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
    let formattedNumber = formatNumber(1234567890);
    console.log("7. 数字格式化：" + formattedNumber);

    // 8. 数字单位转换
    function convertNumber(num) {
      if (num >= 1000000000) {
        return (num / 10_0000_0000).toFixed(1) + "亿";
      } else if (num >= 10_00000) {
        return (num / 10_0000).toFixed(1) + "万";
      } else if (num >= 1000) {
        return (num / 1000).toFixed(1) + "千";
      } else {
        return num.toString();
      }
    }
    let convertedNumber1 = convertNumber(123456789);
    let convertedNumber2 = convertNumber(234324);
    console.log("8. 数字单位转换：" + convertedNumber1, convertedNumber2);

    // 9. 
  </script>
  
</body>
</html>